package response

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

type Response struct {
	Code int         `json:"code"`
	Data interface{} `json:"data"`
	Msg  string      `json:"msg"`
}

func Ok(c *gin.Context, data interface{}) {
	Result(http.StatusOK, data, "获取成功", c)
}

func Msg(c *gin.Context, msg string) {
	c.JSON(http.StatusOK, gin.H{
		"msg":  msg,
		"code": http.StatusOK,
	})
}

func Bad(c *gin.Context, msg string) {
	c.JSON(http.StatusBadRequest, gin.H{"msg": msg, "code": http.StatusBadRequest})
	c.Abort()
}

func Err(c *gin.Context, err error) {
	c.JSON(http.StatusInternalServerError, gin.H{"msg": err.Error(), "code": http.StatusInternalServerError})
	c.Abort()
}

func Unauthorized(c *gin.Context, msg string) {
	c.JSON(http.StatusUnauthorized, gin.H{"msg": msg, "code": http.StatusUnauthorized})
	c.Abort()
}

func OkWithDetailed(c *gin.Context, data interface{}, message string) {
	Result(http.StatusOK, data, message, c)
	c.Abort()
}

func Result(code int, data interface{}, msg string, c *gin.Context) {
	c.JSON(http.StatusOK, Response{
		code,
		data,
		msg,
	})
}

type Page struct {
	Page     int
	PageSize int
	Total    int
	Data     interface{}
}
